<?php

namespace App\Models\Admin;
use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission{
    protected $table = 'permissions';
    //设置主键值
    protected $primaryKey = 'id';
    protected $guarded=['_token'];

    //设置无限上下级//组合一维数组
    public function cateLevel($cate,$fid=0,$html='├─ ',$level = 0){
        $arr = array();
        foreach($cate as $v){
            if($v['fid'] == $fid){
                $v['level'] = $level + 1 ;
                $v['html'] = str_repeat($html,$level);
                $arr[] = $v;
                $arr = array_merge($arr,self::cateLevel($cate,$v['id'],$html,$level+1));
            }
        }
        return $arr;
    }
    //传入一个父级ID获取子级分类ID
    public function getChildId($cate,$fid){
        $arr = array();
        foreach ($cate as $v ){
            if($v['fid'] == $fid){
                $arr[] = $v['id'];
                $arr = array_merge($arr, self::getChildId($cate,$v['id']));
            }
        }
        return $arr;
    }
    //分配权限
    function assignPermissions($permission_Arr,$permission_role = array(),$fid = 0){
        $arr = array();
        foreach ($permission_Arr as $v){
            if(is_array($permission_role)){
                $v['access'] = in_array($v['id'],$permission_role) ? 1:0;
            }
            if($v['fid'] == $fid){
                $v['child'] = $this->assignPermissions($permission_Arr, $permission_role,$v['id']);
                $arr[] = $v;
            }
        }
        return $arr;
    }

    //分配权限
    function getPermission($permission_Arr,$id){
        $arr = array();
        foreach ($permission_Arr as $v){
            if($v['id'] == $id){
                continue;
            }
            $arr[] = $v;
        }
        return $arr;
    }

}